package com.stek.settings;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.MenuItem;
import android.view.View;

import com.google.android.gms.ads.AdView;
import com.stek.OneTouchApplication;
import com.stek.R;
import com.stek.utils.AdMode;
import com.stek.utils.AppUtils;
import com.stek.utils.Broadcast;
import com.stek.utils.CONFIG;

public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener, OnPreferenceClickListener {

	private CheckBoxPreference cbLock;
	/** The view to show the ad. */
	protected AdView adView;

	@Override
	protected void onPause() {
		if (adView != null) {
			adView.pause();
		}
		super.onPause();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		adView = (AdView) findViewById(R.id.admod);
		AdMode.init(adView);
		((OneTouchApplication) getApplication()).getGATracking()
				.sendScreenView(getClass());
		setupSimplePreferencesScreen();
		initActionBar();

	}

	@Override
	protected void onResume() {
		cbLock.setChecked(CONFIG.ADMIN_DEVICE
				.isDeviceAdmin(getApplicationContext()));
		if (adView != null) {
			adView.resume();
		}
		super.onResume();
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private void initActionBar() {
		if (Build.VERSION.SDK_INT >= 11) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
			getActionBar().setBackgroundDrawable(
					new ColorDrawable(getResources().getColor(R.color.blue)));
		} else {
			View title = getWindow().findViewById(android.R.id.title);
			if (title != null) {
				View titleBar = (View) title.getParent();
				titleBar.setBackgroundDrawable(new ColorDrawable(getResources()
						.getColor(R.color.blue)));
			}

		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			onBackPressed();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@SuppressWarnings("deprecation")
	private void setupSimplePreferencesScreen() {
		addPreferencesFromResource(R.xml.pref_general);
		PreferenceManager.getDefaultSharedPreferences(this)
				.registerOnSharedPreferenceChangeListener(this);
		cbLock = (CheckBoxPreference) findPreference(getString(R.string.key_lock));
		cbLock.setOnPreferenceClickListener(this);
		findPreference(getString(R.string.key_uninstall))
				.setOnPreferenceClickListener(this);
		findPreference(getString(R.string.key_feeback))
				.setOnPreferenceClickListener(this);
	}

	@Override
	protected void onDestroy() {
		PreferenceManager.getDefaultSharedPreferences(this)
				.unregisterOnSharedPreferenceChangeListener(this);
		if (adView != null) {
			// Destroy the AdView.
			adView.destroy();
		}
		super.onDestroy();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {

		if (getString(R.string.key_show_float_icon).equals(key)) {
			// Broadcast.sendBroadcast(getApplicationContext(),
			// CONFIG.BROADCAST.ACTION_SHOW_ICON_ON_LOCK_SETTINFS);
			Broadcast.sendBroadcast(getApplicationContext(),
					CONFIG.BROADCAST.ACTION_SHOW_ICON_ON_LOCK_SETTINFS,
					CONFIG.BROADCAST.EXTRA_FLOATING_ICON_SETTINFS,
					sharedPreferences.getBoolean(key, true));
		}
		// if (getString(R.string.key_trust_wifi).equals(key)) {
		// boolean value = sharedPreferences.getBoolean(key, true);
		// if (!value) {
		// CONFIG.LOCK.enable(getApplicationContext());
		// DBHelper.getInstance(getApplicationContext()).deleteTable(
		// DBHelper.WIFI.TABLE);
		// } else {
		// CONFIG.LOCK.disable(getApplicationContext());
		// // showWifi(SettingsActivity.this);
		// Toast.makeText(getApplicationContext(),
		// "Please choose your trust wifi connection",
		// Toast.LENGTH_LONG).show();
		//
		// }
		// }

	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		boolean value = preference.getSharedPreferences().getBoolean(
				preference.getKey(), true);
		if (getString(R.string.key_lock).equals(preference.getKey())) {
			CONFIG.ADMIN_DEVICE.toggleDeviceAdmin(getApplicationContext(),
					value);
			cbLock.setChecked(false);
		} else if (getString(R.string.key_uninstall)
				.equals(preference.getKey())) {
			CONFIG.ADMIN_DEVICE.removeDeviceAdmin(getApplicationContext());
			AppUtils.uninstallApp(getApplicationContext(),
					getApplicationContext().getPackageName());
		} else if (getString(R.string.key_feeback).equals(preference.getKey())) {
			AppUtils.sendEmail(SettingsActivity.this, CONFIG.FEEDBACK.EMAIL,
					CONFIG.FEEDBACK.SUBJECT, "");
		}
		return false;
	}

	// private void showWifi(Context context) {
	// AlertDialog.Builder builder = new AlertDialog.Builder(
	// SettingsActivity.this);
	// View view = LayoutInflater.from(context).inflate(
	// R.layout.view_dialog_wifi, null);
	// builder.setView(view);
	// final AlertDialog alertDialog = builder.create();
	// ((TextView) view.findViewById(R.id.tv_title)).setText(context
	// .getString(R.string.wifi));
	// ListView lv = (ListView) view.findViewById(R.id.lv_wifi);
	// final ArrayList<Wifi> wifis = getAvailableWifi(context);
	// final TrustWifiAdapter adapter = new TrustWifiAdapter(wifis, context);
	// lv.setAdapter(adapter);
	// lv.setOnItemClickListener(new OnItemClickListener() {
	//
	// @Override
	// public void onItemClick(AdapterView<?> parent, View view,
	// int position, long id) {
	// Wifi wifi = (Wifi) parent.getAdapter().getItem(position);
	// if (!wifi.trust) {
	// DBHelper.getInstance(getApplicationContext())
	// .insertTrustWifi(getApplicationContext(), wifi);
	// wifi.trust = true;
	// adapter.notifyDataSetChanged();
	// if (wifi.isConnected) {
	// CONFIG.LOCK.disable(getApplicationContext());
	// }
	//
	// } else {
	// if (wifi.isConnected) {
	// CONFIG.LOCK.enable(getApplicationContext());
	// }
	// wifi.trust = false;
	// adapter.notifyDataSetChanged();
	// DBHelper.getInstance(getApplicationContext())
	// .deleteTrustWifi(getApplicationContext(), wifi);
	//
	// }
	// }
	// });
	//
	// alertDialog.show();
	//
	// }

	/*
	 * private ArrayList<Wifi> getAvailableWifi(Context context) {
	 * ArrayList<Wifi> infos = new ArrayList<Wifi>(); WifiManager wifiManager =
	 * (WifiManager) context .getSystemService(Context.WIFI_SERVICE);
	 * List<ScanResult> results = wifiManager.getScanResults(); ArrayList<Wifi>
	 * trustWifis = DBHelper.getInstance(
	 * getApplicationContext()).getTrustWifi(getApplicationContext()); if (null
	 * == results) { return infos; } String BSSID =
	 * Utils.WIFI.getCurrentBSSID(context);
	 * 
	 * for (ScanResult scanResult : results) { Wifi info = new Wifi();
	 * info.BSSID = scanResult.BSSID; info.SSID = scanResult.SSID; if
	 * (trustWifis.contains(info)) { info.trust = true; if
	 * (info.BSSID.equals(BSSID)) { info.isConnected = true; } }
	 * 
	 * infos.add(info); } return infos; }
	 */
}
